package com.dmz.workflow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.dmz.workflow.base.BaseServiceImpl;
import com.dmz.workflow.constant.LanguageExpressionConstants;
import com.dmz.workflow.constant.MsgContentEnum;
import com.dmz.workflow.dao.WorkflowDiscussionMapper;
import com.dmz.workflow.service.WorkflowDictionaryService;
import com.dmz.workflow.service.WorkflowDiscussionService;
import com.dmz.workflow.service.WorkflowProcessRelationsService;
import com.dmz.workflow.util.BeanUtilsExt;
import com.dmz.workflow.util.ImcComponent;
import com.dmz.workflow.util.MessageContentWrapper;
import com.dmz.workflow.util.ObjectUtils;
import com.dmz.workflow.vo.DictionaryVo;
import com.dmz.workflow.vo.DiscussionVo;
import com.dmz.workflow.wrapper.MemberFeignWrapper;
import com.echronos.commons.Result;
import com.echronos.commons.model.RequestUser;
import com.echronos.commons.utils.RequestUserUtils;
import com.echronos.system.resp.MemberResp;
import com.echronos.workflow.constant.ProcessConstants;
import com.dmz.workflow.entity.Dictionary;
import com.dmz.workflow.entity.Discussion;
import com.dmz.workflow.entity.ProcessRelations;
import io.jsonwebtoken.lang.Collections;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class WorkflowDiscussionServiceImpl extends BaseServiceImpl<WorkflowDiscussionMapper, Discussion> implements WorkflowDiscussionService {

    @Autowired
    private WorkflowDiscussionMapper workflowDiscussionMapper;

    @Autowired
    private WorkflowDictionaryService workflowDictionaryService;

    @Autowired
    private ImcComponent imcComponent;

    @Autowired
    private MessageContentWrapper messageContentWrapper;

    @Autowired
    private WorkflowProcessRelationsService workflowProcessRelationsService;

    @Autowired
    private MemberFeignWrapper memberFeignWrapper;

    @Override
    public List<Discussion> selectDiscussionByProcessInstanceId(String processInstanceId) {
        LambdaQueryWrapper<Discussion> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Discussion::getProcessInstanceId, processInstanceId)
                .orderByAsc(Discussion::getCreateTime);
        return workflowDiscussionMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 添加评论 （不管是附件还是图片）
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveDiscussion(DiscussionVo discussionVO) {
        RequestUser user = RequestUserUtils.getUser();
        discussionVO.setCreateUser(user.getMemberId());
        discussionVO.setCompanyId(user.getCompanyId());
        discussionVO.setProcessInstanceId(discussionVO.getProcessInstanceId());
        Discussion discussion = new Discussion();
        BeanUtils.copyProperties(discussionVO, discussion);
        workflowDiscussionMapper.insert(discussion);
        if (ObjectUtils.isNotEmpty(discussionVO)) {
            List<DictionaryVo> dictionaryVos = discussionVO.getDictionaryVOList();
            if (ObjectUtils.isNotEmpty(dictionaryVos)) {
                for (DictionaryVo dictionary : dictionaryVos) {
                    dictionary.setCode(String.valueOf(discussion.getId()));
                    dictionary.setProcessInstanceId(discussion.getProcessInstanceId());
                    dictionary.setCompanyId(user.getCompanyId());
                    dictionary.setCreateUser(user.getMemberId());
                    dictionary.setBelongTo(ProcessConstants.ATTACHMENT_BELONG_TO_COMMENT);
                }
                List<Dictionary> dictionaries = BeanUtilsExt.copyObjectListProperties(dictionaryVos, Dictionary.class);
                workflowDictionaryService.saveBatch(dictionaries);
            }
        }
        if (!Collections.isEmpty(discussionVO.getAtPersons())) {
            Result<MemberResp> memberById = memberFeignWrapper.getMemberById(RequestUserUtils.getUser().getMemberId());
            imcComponent.imcAssistantSender(
                    messageContentWrapper.generateCommentMessageBody(MsgContentEnum.MESSAGE_COMMENT_CONTENT, discussionVO.getProcessInstanceId(), memberById.getData().getName()),
                    user.getId(), discussionVO.getAtPersons(), null
            );
            workflowProcessRelationsService.save(ProcessRelations.builder().processInstanceId(discussionVO.getProcessInstanceId()).userIds(StringUtils.join(discussionVO.getAtPersons(), LanguageExpressionConstants.LANGUAGE_COMMA)).build());
        }
    }

    @Override
    public void updateBackDiscussion(String processInstanceId, String srcNodeId, String tarNodeId) {
        LambdaUpdateWrapper<Discussion> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Discussion::getProcessInstanceId, processInstanceId)
                .eq(Discussion::getNodeId, srcNodeId);
        Discussion discussion = new Discussion();
        discussion.setNodeId(tarNodeId);
        workflowDiscussionMapper.update(discussion, lambdaUpdateWrapper);
    }

}
